package sort;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 87111
 * Date: 2025-09-14
 * Time: 15:41
 */
public class Sort {

    /**
     * 归并排序
     * 时间复杂度：O(nlogn)
     * 空间复杂度：O(n)
     * 稳定性：稳定
     */
    public void mergeSort(int[] array) {
        merge(array,0,array.length-1);
    }
    private void merge(int[] array, int left, int right) {
        if(left >= right) {
            return;
        }
        //拆分
        int mid = (left + right) / 2;
        merge(array,left,mid);
        merge(array,mid+1,right);
        //合并
        mergeChild(array,left,mid,right);
    }
    private void mergeChild(int[] array, int left, int mid, int right) {
        int s1 = left;
        int e1 = mid;
        int s2 = mid+1;
        int e2 = right;
        //临时数组
        int[] tmp = new int[right - left + 1];
        int k = 0;
        while(s1 <= e1 && s2 <= e2) {
            if(array[s1] < array[s2]) {
                tmp[k++] = array[s1++];
            }else {
                tmp[k++] = array[s2++];
            }
        }
        while(s1 <= e1) {
            tmp[k++] = array[s1++];
        }
        while(s2 <= e2) {
            tmp[k++] = array[s2++];
        }
        //将临时数组的值放到原来数组中
        for (int i = 0; i < tmp.length; i++) {
            array[i+left] = tmp[i];
        }
    }

    //非递归实现归并排序
    public void mergeSortNo(int[] array) {
        int gap = 1;
        while(gap <= array.length) {
            for (int i = 0; i < array.length; i = i + 2*gap) {
                int left = i;
                int mid = left + gap - 1;
                if(mid >= array.length) {
                    mid = array.length-1;
                }
                int right = mid + gap;
                if(right >= array.length) {
                    right = array.length-1;
                }
                mergeChild(array,left,mid,right);
            }
            gap = 2 * gap;
        }
    }

    /**
     * 时间复杂度：O(max(n,范围))
     * 空间复杂度：O(创建数组范围)
     * 稳定性：稳定
     * @param array
     */
    public void CountSort(int[] array) {
        int min = array[0];
        int max = array[0];
        for (int i = 1; i < array.length; i++) {
            if(min > array[i]) {
                min = array[i];
            }
            if(max < array[i]) {
                max = array[i];
            }
        }
        int[] tmp = new int[max-min+1];

        for (int i = 0; i < array.length; i++) {
            tmp[array[i]-min]++;
        }
        int j = 0;
        for (int i = 0; i < tmp.length; i++) {
            while(tmp[i] != 0) {
                array[j++] = i+min;
                tmp[i]--;
            }
        }
    }

    //桶排序
    public void bucketSort(int[] array) {
        //得到数据的最大值和最小值
        int min = array[0];
        int max = array[0];
        for (int i = 1; i < array.length; i++) {
            if(min > array[i]) {
                min = array[i];
            }
            if(max < array[i]) {
                max = array[i];
            }
        }
        //计算有几个桶
        int bucketNum = (max - min) / array.length + 1;
        ArrayList<ArrayList<Integer>> arr = new ArrayList<>(bucketNum);
        for (int i = 0; i < bucketNum; i++) {
            arr.add(new ArrayList<Integer>());
        }
        //将每个元素放到桶中
        for (int i = 0; i < array.length; i++) {
            int num = (array[i] - min) / array.length;
            arr.get(num).add(array[i]);
        }
        //对每个桶进行排序
        for (int i = 0; i < arr.size(); i++) {
            Collections.sort(arr.get(i));
        }
        //将桶里面元素放到原序列中
        int index = 0;
        for (int i = 0; i < arr.size(); i++) {
            for (int j = 0; j < arr.get(i).size(); j++) {
                array[index++] = arr.get(i).get(j);
            }
        }
    }
}
